package com.quickd.web.controller.wms;

import cn.hutool.core.bean.BeanUtil;
import com.quickd.core.annotation.LogOperationAno;
import com.quickd.core.common.JsonResult;
import com.quickd.core.validator.ValidatorUtils;
import com.quickd.core.validator.group.AddGroup;
import com.quickd.service.dto.wms.FormDTO;
import com.quickd.service.entity.wms.*;
import com.quickd.service.dto.wms.FormDetailDTO;
import com.quickd.service.service.wms.*;
import com.quickd.sys.jwt.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.quickd.core.controller.BaseController;
import io.swagger.annotations.Api;

import java.util.List;

/**
 * 单据明细
 *
 * @author quickd quickd@dacanginfo.com
 * @since 2019-10-08
 */
@RestController
@RequestMapping("wms/formDetail")
@Api(tags = "单据明细")
public class FormDetailController extends BaseController<IFormDetailService, FormDetail, FormDetailDTO> {

    @Autowired
    private IStorageBinService iStorageBinService;

    @Autowired
    private IStorageService iStorageService;

    @Autowired
    private IFormDetailService iFormDetailService;

    @PostMapping("addIo")
    @ApiOperation("添加入库单")
    @LogOperationAno("添加入库单")
    public JsonResult addIo(@RequestBody List<FormDetail> formDetailList) {
        FormDetail  formDetail =   formDetailList.get(0);
        String inStorageBinCode = formDetail.getInStorageBinCode();
        StorageBin inStorageBin =  iStorageBinService.queryByCode(inStorageBinCode);
        Storage inStorage = iStorageService.queryByCode(inStorageBin.getStoreCode());
        Form form = new Form();
//        0入库、1出库、2移库、3盘点
        form.setType(0);
        form.setInStorage(inStorage.getCode());
        form.setStatus(1);
        iFormDetailService.addForm(form, formDetailList);
        return new JsonResult().ok("成功添加数据");
    }

    @PostMapping("addOo")
    @ApiOperation("添加出库单")
    @LogOperationAno("添加出库单")
    public JsonResult addOo(@RequestBody List<FormDetail> formDetailList) {
        FormDetail  formDetail =   formDetailList.get(0);
        String outStorageBinCode = formDetail.getOutStorageBinCode();
        StorageBin outStorageBin =iStorageBinService.queryByCode(outStorageBinCode);
        Storage outStorage = (Storage) iStorageService.queryByCode(outStorageBin.getStoreCode());
        Form form = new Form();
//        0入库、1出库、2移库、3盘点
        form.setType(1);
        form.setStatus(1);
        form.setOutStorage(outStorage.getCode());
        iFormDetailService.addForm(form, formDetailList);
        return new JsonResult().ok("成功添加数据");
    }

    @PostMapping("addMio")
    @ApiOperation("添加盘点单")
    @LogOperationAno("添加盘点单")
    public JsonResult addMio(@RequestBody List<FormDetail> formDetailList) {
        FormDetail  formDetail =   formDetailList.get(0);
        String miStorageBinCode = formDetail.getMiStorageBinCode();
        StorageBin miStorageBin =iStorageBinService.queryByCode(miStorageBinCode);
        Storage miStorage = iStorageService.queryByCode(miStorageBin.getStoreCode());
//        0入库、1出库、2移库、3盘点
        Form form = new Form();
        form.setType(3);
        form.setStatus(1);
        form.setMiStorage(miStorage.getCode());
        iFormDetailService.addForm(form, formDetailList);
        return new JsonResult().ok("成功添加数据");
    }

    @PostMapping("addTo")
    @ApiOperation("添加移库单")
    @LogOperationAno("添加移库单")
    public JsonResult addTo(@RequestBody List<FormDetail> formDetailList) {
        FormDetail  formDetail =   formDetailList.get(0);
        String inStorageBinCode = formDetail.getInStorageBinCode();
        String outStorageBinCode = formDetail.getOutStorageBinCode();
        StorageBin inStorageBin =iStorageBinService.queryByCode(inStorageBinCode);
        Storage inStorage = iStorageService.queryByCode(inStorageBin.getStoreCode());

        StorageBin outStorageBin = iStorageBinService.queryByCode(outStorageBinCode);
        Storage outStorage = iStorageService.queryByCode(outStorageBin.getStoreCode());
        Form form = new Form();
//        0入库、1出库、2移库、3盘点
        form.setType(2);
        form.setInStorage(inStorage.getCode());
        form.setStatus(1);
        form.setOutStorage(outStorage.getCode());
        iFormDetailService.addForm(form, formDetailList);
        return new JsonResult().ok("成功添加数据");
    }


}